/**
 * 
 */
package action.admin;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

import model.TeacherDAO;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.opensymphony.xwork2.ActionSupport;

import util.ImportTeacher;
import model.*;
/** 
 * @作者 Zouzr 
 * @创建日期 Dec 10, 2008 
 * @版本 V 1.0 
 */

public class TeacherAction extends ActionSupport
		{
		private String actiontype="1";//actiontype用于标识是获取所有的记录还是搜索记录,默认是获取所有记录即”1“
		
		private String actionname = "getallteachers";//action名称
	
		private String id;//教师表id
		
		private String teachername;//教师姓名
	
		private String teachersex;//教师性别
	
		private int currentpage=1;//当前页数,当获得的值为null或""时初始化为1;
	
		private int pageresult=20;//每页显示数据的条数,当获得的值为null或""时初始化为20;
		
		private int pagecount;//总页数
		
		private int resultcount;//总记录
	
	 	private File upload;// 实际上传文件

	    private String uploadContentType; // 文件的内容类型

	    private String uploadFileName; // 上传文件名
	    
	    private List<Teacher> teachers = new ArrayList<Teacher>();//存放所有的teacher记录
	    
	    private ServletActionContext context;
	    
	    //保存上传文件的目录，相对于web应用程序的根路径，在struts.xml文件中配置
	    private String uploadDir;

	    /**
	     * 验证数据合法性
	     */
	    public void validate()
	    {	
	    	//this.addFieldError("msg.hello","必须输入hello!");
	    	//this.addActionError("处理动作失败!");
	    	//this.addActionMessage("提交成功");
	    }
	    
	    /**
	     * 上传教师信息excel文件，并将文件数据导入到数据库中
	     * @return
	     * @throws Exception
	     */
	    public String importTeacherInfo() throws Exception {
	        try {
	        	if(uploadFileName==null)
	        	{
	        		this.addActionError("无效的文件路径!");
            		return INPUT;
	        	}
	        	System.out.println("文件类型"+uploadContentType);
	        	System.out.println("文件上传名"+uploadFileName);
	            String targetDirectory = context.getServletContext().getRealPath(uploadDir);
	            System.out.println("目标路径:"+targetDirectory);
	            String targetFileName = uploadFileName;
	            File target = new File(targetDirectory, targetFileName);
	            FileUtils.copyFile(upload, target);    
	            setUploadFileName(target.getPath());//保存文件的存放路径
		        } 
	        	catch (Exception e) 
	        	{
		        	System.out.println(e.getMessage());
		        }
	        	//先将数据库中原来的教师的记录都删除，再导入新的所有教师的信息
	        	ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
		        TeacherDAO teacherdao = (TeacherDAO)context.getBean("TeacherDAOProxy");
		        teacherdao.deleteAllTeacherInfo();
	        	//将上传的xls中的数据插入到数据库中
	        	String returnstring=ImportTeacher.importIntoTeacher(getUploadFileName());
	        	if(!returnstring.equals(""))
	        	{
	        		this.addActionError(returnstring);
	        		return INPUT;
	        	}
	        return SUCCESS;

	    }
	    
	    public String getTeacherById()
	    {
	    	ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
	        TeacherDAO teacherdao = (TeacherDAO)context.getBean("TeacherDAOProxy");
	        List temp_teachers = teacherdao.getTeacherById(id);
	       
	        for(int i=0;i<temp_teachers.size();i++)
	        {
	        	Teacher t = new Teacher();
	        	t=(Teacher)temp_teachers.get(i);
	        	int begin=(currentpage-1)*pageresult;
	        	int end=currentpage*pageresult;
	        	if((begin<=i)&&(i<end))
	        	{
	        		teachers.add(t);
	        	}
	        	t=null;
	        }
	        return SUCCESS;
	    }

	    /**
	     * 根据分页信息得到非查询的数据
	     */
	    public String getAllTeachers()
	    {
	    	//System.out.println(context.getRequest().getParameter("currentpage"));
	    	//System.out.println(context.getRequest().getParameter("pageresult"));
	    	ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
	        TeacherDAO teacherdao = (TeacherDAO)context.getBean("TeacherDAOProxy");
	        List temp_teachers = teacherdao.findAll();
	       
	        for(int i=0;i<temp_teachers.size();i++)
	        {
	        	Teacher t = new Teacher();
	        	t=(Teacher)temp_teachers.get(i);
	        	int begin=(currentpage-1)*pageresult;
	        	int end=currentpage*pageresult;
	        	if((begin<=i)&&(i<end))
	        	{
	        		teachers.add(t);
	        	}
	        	t=null;
	        }
	        //设置总页数
	        if(temp_teachers.size()%pageresult==0)
	        	setPagecount(temp_teachers.size()/pageresult);
	        else
	        	setPagecount(temp_teachers.size()/pageresult+1);
	        //设置总记录数
	        setResultcount(temp_teachers.size());
	        return SUCCESS;
	    }
	    
	    /**
	     * 根据分页信息得到查询的数据
	     */
	    public String searchTeachers()
	    {
	    	setActiontype("2");//设置action类型为搜索
	    	setActionname("searchteachers");//设置action名称为搜索
	    	
	    	System.out.println("获得的teachername为："+teachername);
	    	System.out.println("获得的teachersex为："+teachersex);
	    	System.out.println("获得的currentpage为："+currentpage);
	    	System.out.println("获得的pageresult为："+pageresult);
	    	ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
	        TeacherDAO teacherdao = (TeacherDAO)context.getBean("TeacherDAOProxy");
	        List temp_teachers = teacherdao.searchTeachers(teachername,teachersex);
	        System.out.println(temp_teachers.size());
	        for(int i=0;i<temp_teachers.size();i++)
	        {
	        	Teacher t = new Teacher();
	        	t = (Teacher)temp_teachers.get(i);
	        	int begin=(currentpage-1)*pageresult;
	        	int end=currentpage*pageresult;
	        	if((begin<=i)&&(i<end))
	        	{
	        		teachers.add(t);
	        	}
	        	t=null;
	        }
	      //设置总页数
	        if(temp_teachers.size()%pageresult==0)
	        	setPagecount(temp_teachers.size()/pageresult);
	        else
	        	setPagecount(temp_teachers.size()/pageresult+1);
	      //设置总记录数
	        setResultcount(temp_teachers.size());
	        return SUCCESS;
	    }
	    
	    public File getUpload() {
	        return upload;
	    }

	    public void setUpload(File upload) {
	        this.upload = upload;
	    }

	    public String getUploadContentType() {
	        return uploadContentType;
	    }

	    public void setUploadContentType(String uploadContentType) {
	        this.uploadContentType = uploadContentType;
	    }

	    public String getUploadFileName() {
	        return uploadFileName;
	    }

	    public void setUploadFileName(String uploadFileName) {
	        this.uploadFileName = uploadFileName;
	    }

		/**
		 * @return the uploadDir
		 */
		public String getUploadDir() {
			return uploadDir;
		}

		/**
		 * @param uploadDir the uploadDir to set
		 */
		public void setUploadDir(String uploadDir) {
			this.uploadDir = uploadDir;
		}

		/**
		 * @return the teachers
		 */
		public List<Teacher> getTeachers() {
			return teachers;
		}

		/**
		 * @param teachers the teachers to set
		 */
		public void setTeachers(List<Teacher> teachers) {
			this.teachers = teachers;
		}

		/**
		 * @return the currentpage
		 */
		public int getCurrentpage() {
			return currentpage;
		}

		/**
		 * @param currentpage the currentpage to set
		 */
		public void setCurrentpage(int currentpage) {
			this.currentpage = currentpage;
		}

		/**
		 * @return the pageresult
		 */
		public int getPageresult() {
			return pageresult;
		}

		/**
		 * @param pageresult the pageresult to set
		 */
		public void setPageresult(int pageresult) {
			this.pageresult = pageresult;
		}

		/**
		 * @return the teachername
		 */
		public String getTeachername() {
			return teachername;
		}

		/**
		 * @param teachername the teachername to set
		 */
		public void setTeachername(String teachername) {
			this.teachername = teachername;
		}

		/**
		 * @return the teachersex
		 */
		public String getTeachersex() {
			return teachersex;
		}

		/**
		 * @param teachersex the teachersex to set
		 */
		public void setTeachersex(String teachersex) {
			this.teachersex = teachersex;
		}

		/**
		 * @return the actiontype
		 */
		public String getActiontype() {
			return actiontype;
		}

		/**
		 * @param actiontype the actiontype to set
		 */
		public void setActiontype(String actiontype) {
			this.actiontype = actiontype;
		}

		/**
		 * @return the pagecount
		 */
		public int getPagecount() {
			return pagecount;
		}

		/**
		 * @param pagecount the pagecount to set
		 */
		public void setPagecount(int pagecount) {
			this.pagecount = pagecount;
		}

		/**
		 * @return the resultcount
		 */
		public int getResultcount() {
			return resultcount;
		}

		/**
		 * @param resultcount the resultcount to set
		 */
		public void setResultcount(int resultcount) {
			this.resultcount = resultcount;
		}

		/**
		 * @return the actionname
		 */
		public String getActionname() {
			return actionname;
		}

		/**
		 * @param actionname the actionname to set
		 */
		public void setActionname(String actionname) {
			this.actionname = actionname;
		}

		/**
		 * @return the id
		 */
		public String getId() {
			return id;
		}

		/**
		 * @param id the id to set
		 */
		public void setId(String id) {
			this.id = id;
		}


}
